// Developed by Nguyen Duc Minh (minhnot@gmail.com)
var mVd = new Array
(
	new Array('\u00E2','a','\u0103','\u00EA','e','i','\u00F4','o','\u01A1','u','\u01B0','y','\u00C2','A','\u0102','\u00CA','E','I','\u00D4','O','\u01A0','U','\u01AF','Y'),
	new Array('\u1EA5','\u00E1','\u1EAF','\u1EBF','\u00E9','\u00ED','\u1ED1','\u00F3','\u1EDB','\u00FA','\u1EE9','\u00FD','\u1EA4','\u00C1','\u1EAE','\u1EBE','\u00C9','\u00CD','\u1ED0','\u00D3','\u1EDA','\u00DA','\u1EE8','\u00DD'),
	new Array('\u1EA7','\u00E0','\u1EB1','\u1EC1','\u00E8','\u00EC','\u1ED3','\u00F2','\u1EDD','\u00F9','\u1EEB','\u1EF3','\u1EA6','\u00C0','\u1EB0','\u1EC0','\u00C8','\u00CC','\u1ED2','\u00D2','\u1EDC','\u00D9','\u1EEA','\u1EF2'),
	new Array('\u1EA9','\u1EA3','\u1EB3','\u1EC3','\u1EBB','\u1EC9','\u1ED5','\u1ECF','\u1EDF','\u1EE7','\u1EED','\u1EF7','\u1EA8','\u1EA2','\u1EB2','\u1EC2','\u1EBA','\u1EC8','\u1ED4','\u1ECE','\u1EDE','\u1EE6','\u1EEC','\u1EF6'),
	new Array('\u1EAB','\u00E3','\u1EB5','\u1EC5','\u1EBD','\u0129','\u1ED7','\u00F5','\u1EE1','\u0169','\u1EEF','\u1EF9','\u1EAA','\u00C3','\u1EB4','\u1EC4','\u1EBC','\u0128','\u1ED6','\u00D5','\u1EE0','\u0168','\u1EEE','\u1EF8'),
	new Array('\u1EAD','\u1EA1','\u1EB7','\u1EC7','\u1EB9','\u1ECB','\u1ED9','\u1ECD','\u1EE3','\u1EE5','\u1EF1','\u1EF5','\u1EAC','\u1EA0','\u1EB6','\u1EC6','\u1EB8','\u1ECA','\u1ED8','\u1ECC','\u1EE2','\u1EE4','\u1EF0','\u1EF4')
);
var mVs,mVm,mVb,mVl;mVm=1;mVl=0
if(document.all)mVb=0;else if(document.getElementById)mVb=1;else mVb=2;

function mChangeMode(m){mVm=m;}
function mV01(c,k)
{
	for(i=0;i<6;i++)
	{
		for(j=0;j<24;j++)
		{	
			if(c==mVd[i][j])
			{
				if(k=='z' ||k=='Z')
				{
					if(i!=0)return mVd[0][j]
					else return mVd[0][j]+k;
				}
				else if('aeoAEO'.indexOf(k)>=0)
				{
					if('aeo'.indexOf(k)>=0){if(j>11)j=j-12}
					else if(j<12)j=j+12
					if(k=='a'||k=='A')
					{
						if(j==1||j==13)return mVd[i][j-1]
						else 
							if(j==0||j==12)
							{
								if(i==0){mVl=1;return k+k}
								else return mVd[i][j+1]
							}
							else if(j==2||j==14)return mVd[i][j-2]
					}
					else if(k=='o'||k=='O')
					{
						if(j==7||j==19)return mVd[i][j-1]
						else 
							if(j==6||j==18)
							{
								if(i==0){mVl=1;return k+k}
								else return mVd[i][j+1]
							}
							else if(j==8||j==20)return mVd[i][j-2]
					}
					else
					{
						if(j==4||j==16)return mVd[i][j-1]
						else 
							if(j==3||j==15)
							{
								if(i==0){mVl=1;return k+k}
								else return mVd[i][j+1]
							}
					}
				}
				else if(k=='w'||k=='W')
				{
					if(k=='w'){if(j>11)j=j-12}
					else if(j<12)j=j+12
					if(j==1||j==7||j==9||j==13||j==19||j==21)return mVd[i][j+1]
					else
						if(j==2||j==8||j==10||j==14||j==20||j==22)
						{
							if(i==0)
							{
								if(j!=10&&j!=22)return mVd[i][j-1]+mVd[i][j-1]
								else {mVl=1;return k}
							}
							else return mVd[i][j-1]
						}
						else if(j==0||j==6||j==12||j==18)return mVd[i][j+2]
				}
				return c+k;
			}	
		}	
	}
	if(k=='w'||k=='W')
	{
		if(k=='w')return c+'\u01B0'
		else return c+'\u01AF'
	}
	else if(k=='d'||k=='D')
	{
		if(k=='d')
		{
			if(c=='d'||c=='D')return '\u0111'
			else if(c=='\u0111'||c=='\u0110')return 'dd'
		}
		else
		{
			if(c=='d'||c=='D')return '\u0110'
			else if(c=='\u0111'||c=='\u0110')return 'DD'
		}
	}
	return c+k;
}
function mV02(c,k)
{
	if(k<9)
	{
		for(i=0;i<6;i++)
		{
			for(j=0;j<24;j++)
			{	
				if(c==mVd[i][j])
				{
					if(k==6)
					{
						if(j==1||j==4||j==7||j==13||j==16||j==19)return mVd[i][j-1]
						else if(j==2||j==8||j==14||j==20)return mVd[i][j-2]
						else if(j==0||j==3||j==6||j==12||j==15||j==18){mVl=1;return mVd[i][j+1]+k}
					}
					else if(k==7)
					{
						if(j==9||j==7||j==21||j==19||j==1||j==13)return mVd[i][j+1]
						else if(j==2||j==8||j==10||j==14||j==20||j==22){mVl=1;return mVd[i][j-1]+k}
					}
					else if(k==8)
					{
						if(j==1||j==13)return mVd[i][j+1]
						else if(j==0||j==12)return mVd[i][j+2]
						else if(j==2||j==14){mVl=1;return mVd[i][j-1]+k}
					}
				}	
			}	
		}
	}
	else
	{
		if(c=='d')return '\u0111'
		else if(c=='D')return '\u0110'
		else if(c=='\u0111')return 'd9'
		else if(c=='\u0110')return 'D9'
	}
	return c+k;
}
function mV03(c)
{
	for(i=0;i<6;i++)
		for(j=0;j<24;j++)
			if(c==mVd[i][j])return i
	return -1
}
function mV04(c1,c2)
{
	c1=c1.toLowerCase()
	c2=c2.toLowerCase()
	if((c1=='i'||c1=='y')&&(c2=='\u00EA'))return true
	else if((c1=='o')&&(c2=='a'||c2=='e'||c2=='\u00E2'||c2=='\u0103'))return true
	else if((c1=='u')&&(c2=='y'||c2=='\u01A1'||c2=='\u00EA'||c2=='\u00E2'||c2=='\u00F4'))return true
	else if(c1=='\u01B0'&&c2=='\u01A1')return true
	else return false
}
function mV05(c,l,k)
{
	mVs=''
	for(j=0;j<24;j++)
	{
		if(c==mVd[l][j])
		{
			if((k=='s'||k=='S'||k==1)&&(l!=1))return mVd[1][j]
			else if((k=='f'||k=='F'||k==2)&&(l!=2))return mVd[2][j]
			else if((k=='r'||k=='R'||k==3)&&(l!=3))return mVd[3][j]
			else if((k=='x'||k=='X'||k==4)&&(l!=4))return mVd[4][j]
			else if((k=='j'||k=='J'||k==5)&&(l!=5))return mVd[5][j]
			else 
			{
				if(mVm==1)
				{
					if(k=='w'||k=='W')
					{
						if(j==0||j==6||j==12||j==18)return mVd[l][j+2]
						else if(j==1||j==7||j==9||j==13||j==19||j==21)return mVd[l][j+1]
						else if(j==2||j==8||j==10||j==14||j==20||j==22)return mVd[l][j-1]
						else 
						{
							if(k=='w')mVs = '\u01B0'
							else if(k=='W')mVs = '\u01AF'
							else mVs=k;
							return c;
						}
					}
					else 	if('aeoEAO'.indexOf(k)!=-1)
					{
						if(j==0||j==3||j==6||j==12||j==15||j==18)return mVd[l][j+1]
						else if(j==1||j==4||j==7||j==13||j==16||j==19)return mVd[l][j-1]
						else mVs=k;return c;
					}
					else
					{
						if('zZ'.indexOf(k)>=0)
						{
							if(l==0)mVs=k;
							return mVd[0][j]
						}
						else
						{
							mVl=1;mVs=k;
							return mVd[0][j]
						}
					}
				}
				else
				{
					if(k=='7')
					{
						if(j==6||j==18)return mVd[l][j+2]
						else if(j==7||j==9||j==19||j==21)return mVd[l][j+1]
						else if(j==8||j==10||j==20||j==22)return mVd[l][j-1]
						else 	{mVs=k;return c;}
					}
					else if(k=='8')
					{
						if(j==0||j==12)return mVd[l][j+2]
						else if(j==1||j==13)return mVd[l][j+1]
						else if(j==2||j==14)return mVd[l][j-1]
						else 	{mVs=k;return c;}
					}
					else 	if(k=='6')
					{
						if(j==0||j==3||j==6||j==12||j==15||j==18)return mVd[l][j+1]
						else if(j==1||j==4||j==7||j==13||j==16||j==19)return mVd[l][j-1]
						else mVs=k;return c;
					}
					else
					{
						if(k=='0'){if(l==0)mVs=k;return mVd[0][j]}
						else
						{
							mVl=1;mVs=k;
							return mVd[0][j]
						}
					}
				}
			}
		}
	}
}
function mV06(w,k)
{
	var c1,c2
	l=w.length
	c1=w.charAt(l-1)
	i=mV03(c1)
	if((i==-1)&&('tghmncpiuTGHMNCPIU'.indexOf(c1)!=-1))
	{
		if(l==1)
		{
			if(k=='w')return w+'\u01B0'
			else if(k=='W')return w+'\u01AF'
			else 	return w+k
		}
		c2=w.charAt(l-2)
		if(c1=='h'||c1=='H')
		{
			if((l<3)||('ncNC'.indexOf(c2)==-1))
			{
				if(k=='w')return w+'\u01B0'
				else if(k=='W')return w+'\u01AF'
				else return w+k
			}
			l=l-2
		}
		else if(c1=='g'||c1=='G')
		{
			if((l<3)||((c2!='n')&&c2!='N'))
			{
				if(k=='w')return w+'\u01B0'
				else if(k=='W')return w+'\u01AF'
				else return w+k
			}
			l=l-2
		}
		else
		{
			if(l<2)return w+k
			l=l-1
			if(('tnpmcTNPMC'.indexOf(c1)!=-1)&&(w.length>1)&&(c2!=' '))
			{
				i=mV03(c2)
				if((i>=0)&&(('sfrjxwSFRJXW'.indexOf(k)>=0)||(c2==k)))return w.substring(0,l-1)+mV05(c2,i,k)+c1+mVs
				else return w+k
			}
		}
		c1=w.charAt(l-1)
		i=mV03(c1)
		if(i==-1)
		{
			if(k=='w')return w+'\u01B0'
			else if(k=='W')return w+'\u01AF'
			else return w+k
		}
	}
	if((l>1)&&(i==0))
	{
		c2=w.charAt(l-2)
		lc2=mV03(c2)
		if(lc2==-1)return w.substring(0,l-1)+mV05(c1,0,k)+w.substring(l)+mVs
		else
		{
			if((l>1)&&(lc2==0))
			{
				if(mV04(c2,c1)||((c2=='u'||c2=='U')&&(w.charAt(l-3).toLowerCase()=='q'))||((c2=='i'||c2=='I')&&(w.charAt(l-3).toLowerCase()=='g')))
					return w.substring(0,l-1)+mV05(c1,i,k)+w.substring(l)+mVs
				else return w.substring(0,l-2)+mV05(c2,lc2,k)+w.substring(l-1)+mVs
			}	
			else
			{
				if(lc2>0)return w.substring(0,l-2)+mV05(c2,lc2,k)+w.substring(l-1)+mVs
				else
				{
					if(mV04(c2,c1))return c2+mV05(c1,0,k)
					else return mV05(c2,lc2,k)+c1+mVs
				}
			}
		}
	}
	else if(i!=-1)return w.substring(0,l-1)+mV05(c1,i,k)+w.substring(l)+mVs
	else return w+k
}
/*support iframe
<body>
<iframe name="mName" src="about:blank"></iframe>
<script language='javascript' src='mVietNet.js'></script>
<script language='javascript'>
if(document.all)
{
	frames["mName"].document.designMode="on"
	frames["mName"].document.onkeypress=function(){mViet(frames["mName"].event);}
}
else
{
	mIx=window.document.getElementsByName("mName");
	mIx[0].contentWindow.document.designMode="on"
	mIx[0].contentWindow.addEventListener("keypress",mViet,false)
}
</script>
</body>
*/
function mViet(e)
{
	if(mVm==1)
	{
		if(mVb==0)
		{
			if(!e)e=window.event;
			k=String.fromCharCode(e.keyCode)
			o = document.selection.createRange()
			if(mVl==0)
			{
				o.moveStart("word",-1)
				w=o.duplicate().text
				l=w.length
				if((l>0)&&('sfrxjaeowdzSFRXJAEOWDZ'.indexOf(k)!=-1))
				{
					i=w.charAt(l-1)
					if('aeodwzAEODWZ'.indexOf(k)!=-1)
					{	
						if('tghmncpiuTGHMNCPIU'.indexOf(i)==-1)
						{
							if('zZ'.indexOf(k)==-1)o.text=w.substring(0,l-1)+mV01(i,k)
							else o.text=mV06(w,k)
						}
						else o.text=mV06(w,k)
					}
					else o.text=mV06(w,k)
					o.collapse(false)
					e.keyCode=null
				}
				else if((l==0)&&(k=='w'||k=='W'))
				{
					o.collapse(false)
					if(k=='w')o.text='\u01B0'
					else o.text='\u01AF'
					e.keyCode=null
				}
			}
			else
			{
				if(k==' ')mVl=0
				else
				{
					o.moveStart("character",-1)
					w=o.duplicate().text
					if((w==' ')||(w.length==0))mVl=0
				}
			}
		}
		else if((mVb==1)&&!(e.ctrlKey))
		{
			if(mVl==0)
			{
				k=String.fromCharCode(e.which)
				O=e.currentTarget;
				try
				{
					o=O.getSelection().getRangeAt(0)
					var T=o.endContainer;
					if(!T.data)
					{
						if(k=='w'||k=='W')
						{
							T = document.createTextNode("");
							if(k=='w')T.insertData(0,'\u01B0')
							else T.insertData(0,'\u01AF')
							o.insertNode(T);
							o.setEnd(T,1)
							o.setStart(T,1);
							e.preventDefault()
						}
					}
					else
					{
						if('sfrxjaeowdzSFRXJAEOWDZ'.indexOf(k)!=-1)
						{
							l=o.endOffset
							if(l>0)
							{
								R=T.data.substr(l)
								T.deleteData(l,T.data.length)
								o.setEnd(T,l)
								if(l>4)o.setStart(T,l-4)
								else o.setStart(T,0)
								w=T.data;
								i=w.charAt(l-1)
								if('aeodwzAEODWZ'.indexOf(k)!=-1)
								{
									if('tghmncpiuTNGMNCPIU'.indexOf(i)==-1)
									{
										if('zZ'.indexOf(k)==-1)w=w.substring(0,l-1)+mV01(i,k)
										else w=mV06(w,k)
									}
									else w=mV06(w,k)
								}
								else w=mV06(w,k)
								T.data=w+R
								l=w.length;
								o.setEnd(T,l)
								o.setStart(T,l);
								e.preventDefault()
							}
							else if((l==0)&&(k=='w'||k=='W'))
							{
								if(k=='w')T.data='\u01B0'+T.data
								else T.data='\u01AF'+T.data
								o.setEnd(T,1)
								o.setStart(T,1);
								e.preventDefault()
							}
						}
					}
				}
				catch(ee)
				{
					ix=O.selectionStart
					t=O.scrollTop
					iz=4;if(ix<4)iz=ix;
					R=O.value;
					if(ix==0)
					{
						if(k=='w'||k=='W')
						{
							if(k=='w')O.value='\u01B0'
							else O.value='\u01AF'
							O.scrollTop=t
							e.preventDefault()
						}
					}
					else
					{
						w=R.substring(ix-iz,ix);
						l=w.lastIndexOf(" ");
						if(l>0){iz=iz-l-1;w=w.substring(l+1);}
						l=w.length;
						if('sfrxjaeowdzSFRXJAEOWDZ'.indexOf(k)!=-1)
						{
							if(l>0)
							{
								i=w.charAt(l-1)
								if('aeodwzAEODWZ'.indexOf(k)!=-1)
								{
									if('tghmncpiuTNGMNCPIU'.indexOf(i)==-1)
									{
										if('zZ'.indexOf(k)==-1)w=w.substring(0,l-1)+mV01(i,k)
										else w=mV06(w,k)
									}
									else w=mV06(w,k)
								}
								else w=mV06(w,k)
								O.value=R.substring(0,ix-iz)+w+R.substring(ix)
								O.setSelectionRange(ix-l+w.length,ix-l+w.length)
								O.scrollTop=t
								e.preventDefault()
							}
							else if((l==0)&&(k=='w'||k=='W'))
							{
								if(k=='w')O.value=R.substring(0,ix-iz)+'\u01B0'+R.substring(ix)
								else O.value=R.substring(0,ix-iz)+'\u01AF'+R.substring(ix)
								O.setSelectionRange(ix+1,ix+1)
								O.scrollTop=t
								e.preventDefault()
							}
						}
					}
				}
			}
			else {if((e.which==32)||(e.which==8)||(e.which==13))mVl=0}
		}
	}	
	else if(mVm==2)
	{
		if(mVb==0)
		{
			o = document.selection.createRange()
			if(mVl==0)
			{
				o.moveStart("word",-1)
				w=o.duplicate().text
				l=w.length
				if(!e)e=window.event
				k=String.fromCharCode(e.keyCode)
				if((l>0)&&('1234567890'.indexOf(k)!=-1))
				{
					i=w.charAt(l-1)
					if('6789'.indexOf(k)!=-1)
					{
						if('tghmncpiuTNGMNCPIU'.indexOf(i)==-1)o.text=w.substring(0,l-1)+mV02(i,k)
						else o.text=mV06(w,k)
					}
					else o.text=mV06(w,k)
					o.collapse(false);
					e.keyCode=null
				}
			}
			else
			{
				o.moveStart("character",-1)
				w=o.duplicate().text
				if((w==' ')||(w.length==0))mVl=0
			}
		}
		else if((mVb==1)&&!(e.ctrlKey))
		{
			if(mVl==0)
			{
				k=String.fromCharCode(e.which)
				O= e.currentTarget;
				try
				{
					o=O.getSelection().getRangeAt(0)
					var T=o.endContainer;
					if(T.data)
					{
						l=o.endOffset
						if((l>0)&&('1234567890'.indexOf(k)!=-1))
						{
							R=T.data.substr(l)
							o.setEnd(T,l)
							o.setStart(T,0)
							T.deleteData(l,T.data.length)
							w=T.data;
							i=w.charAt(l-1)
							if('6789'.indexOf(k)!=-1)
							{
								if('tghmncpiuTNGMNCPIU'.indexOf(i)==-1)w=w.substring(0,l-1)+mV02(i,k)
								else w=mV06(w,k)
							}
							else w=mV06(w,k)
							T.data=w+R
							l=w.length;
							o.setEnd(T,l)
							o.setStart(T,l);
							e.preventDefault()
						}
					}
				}
				catch(ee)
				{
					ix=O.selectionStart
					t=O.scrollTop
					iz=4;if(ix<4)iz=ix;
					R=O.value;
					if(ix>0)
					{
						w=R.substring(ix-iz,ix);
						l=w.lastIndexOf(" ");
						if(l>0){iz=iz-l-1;w=w.substring(l+1);}
						l=w.length;
						if('1234567890'.indexOf(k)!=-1)
						{
							i=w.charAt(l-1)
							if('6789'.indexOf(k)!=-1)
							{
								if('tghmncpiuTNGMNCPIU'.indexOf(i)==-1)w=w.substring(0,l-1)+mV02(i,k)
								else w=mV06(w,k)
							}
							else w=mV06(w,k)
							O.value=R.substring(0,ix-iz)+w+R.substring(ix)
							O.setSelectionRange(ix-l+w.length,ix-l+w.length)
							O.scrollTop=t
							e.preventDefault()
						}
					}
				}
			}
			else {if((e.which==32)||(e.which==8)||(e.which==13))mVl=0}
		}
	}	
}